package com.wangp.mywrite.s_juc.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author farling-wangp
 * @version 1.0
 * @date 2020/10/11 15:08
 * AtomicInteger基本用法
 */
public class AtomicIntegerDemo1 implements Runnable{
    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
    public void incrementAtomic(){
        ATOMIC_INTEGER.getAndAdd(2);
    }
    private static volatile int basicCount = 0;
    public void incrementBasic(){
        basicCount++;
    }

    @Override
    public void run() {
        for (int i = 0; i < 2_000_00; i++) {
            incrementAtomic();
            incrementBasic();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        AtomicIntegerDemo1 atomicIntegerDemo1 = new AtomicIntegerDemo1();
        Thread t1 = new Thread(atomicIntegerDemo1);
        Thread t2 = new Thread(atomicIntegerDemo1);
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("原子类的结果是："+ATOMIC_INTEGER.getAndIncrement());
        System.out.println("基础类的结果是："+basicCount);
    }
}
